<?php
require_once (APPLICATION_DIR."/application/model/News.php");
class News extends DataObjects_News
{	
	function getNewsInfoByID($news_id)
	{
		$this->selectAdd();
		$this->selectAdd("news_id,news_title,news_subtitle,news_content,DATE_FORMAT(news_time , '%H:%i %M %D %Y') as news_time,news_hide,DATE_FORMAT(news_modified , '%H:%i %M %D %Y') as news_modified,news_icon,news_order");
		$this->whereAdd();
		$this->whereAdd("news_id = $news_id");
		$this->find();
		return $this;
	}
	
	function getShownedNewsInfoByID($news_id)
	{
		$this->selectAdd();
		$this->selectAdd("news_id,news_title,news_subtitle,news_content,DATE_FORMAT(news_time , '%H:%i %M %D %Y') as news_time,news_hide,DATE_FORMAT(news_modified , '%H:%i %M %D %Y') as news_modified,news_icon,news_order");
		$this->whereAdd();
		$this->whereAdd("news_id = $news_id");
		$this->whereAdd("news_hide = 0");
		$this->find();
		return $this;
	}
	
	function getAllNewsInfo()
	{
		$this->selectAdd();
		$this->selectAdd("news_id,news_title,news_subtitle,DATE_FORMAT(news_time , '%H:%i %M %D %Y') as news_time,news_hide,DATE_FORMAT(news_modified , '%H:%i %M %D %Y') as news_modified,news_order, news_icon, news_order, news_homepage");
		$this->orderBy("news_order DESC");
		$this->find();
		return $this;
	}
	
	function getAllShowNews()
	{
	    $this->selectAdd();
		$this->selectAdd("news_id,news_title,news_subtitle,DATE_FORMAT(news_time , '%H:%i %M %D %Y') as news_time,DATE_FORMAT(news_modified , '%H:%i %M %D %Y') as news_modified,news_order, news_icon, news_order");
		$this->whereAdd();
		$this->whereAdd("news_hide = 0");
		$this->orderBy();
		$this->orderBy("news_order DESC");
		$this->find();
		return $this;
	}
	
	function getHighestOrderNewsID()
	{
	    $this->selectAdd();
		$this->selectAdd("news_id");
		$this->whereAdd();
		$this->whereAdd("news_hide = 0");
		$this->orderBy();
		$this->orderBy("news_order DESC");
		$this->find();
		if ($this->fetch())
		{
		    $result = $this->news_id;
		}
		else 
		{
		    $result = 0;
		}
		return $result;
	}
	
	function getHomepageNews()
	{
	    $this->selectAdd();
		$this->selectAdd("news_id,news_title,news_subtitle,DATE_FORMAT(news_time , '%H:%i %M %D %Y') as news_time,DATE_FORMAT(news_modified , '%H:%i %M %D %Y') as news_modified,news_icon");
		$this->whereAdd();
		$this->whereAdd("news_hide = 0");
		$this->whereAdd("news_homepage = 1");
		$this->orderBy();
		$this->orderBy("news_order DESC");
                $this->limit(3);
		$this->find();
		return $this->fetchList();
	}
	
	function updateNews($news_id,$news_title,$news_subtitle,$news_content,$news_hide, $news_order, $news_icon)
	{
		$this->news_id        = $news_id;
		$this->news_title     = $news_title;
		$this->news_subtitle  = $news_subtitle;
		$this->news_modified  = date("Y-m-d h:m:s");
		$this->news_content   = $news_content;
		$this->news_hide      = $news_hide;
		$this->news_order     = $news_order;
		$this->news_icon      = $news_icon;
		$this->update();
	}
	
	function addNews($news_title,$news_subtitle,$news_content,$news_hide,$news_order,$news_icon)
	{
		$this->news_title     = $news_title;
		$this->news_subtitle  = $news_subtitle;
		$this->news_time      = date("Y-m-d h:m:s");
		$this->news_content   = $news_content;
		$this->news_modified  = $this->news_time;
		$this->news_hide      = $news_hide;
		$this->news_order     = $news_order;
		if ($news_icon)
		{
		    $this->news_icon  = $news_icon;
		}
		$this->insert();
	}
	
	function getNextNewsOrder()
	{
	    $this->selectAdd();
	    $this->selectAdd("news_order");
	    $this->orderBy("news_order DESC");
	    $this->find();
	    if ($this->fetch())
	    {
	        $result = $this->news_order;
	    }
	    else 
	    {
	        $result = 0;
	    }
	    $result++;
	    return $result;
	}
	
	function deleteNews($news_id)
	{
	    $news = new News();
		$news->deleteNewsIconFile($news_id);
	    $this->news_id = $news_id;
	    $this->delete();
	}
	
	function tooglePublish($news_id,$publish)
	{
	    $this->news_id = $news_id;
	    $this->news_homepage = 1 - $publish;
	    $this->update();
	}
	
	function getNextNewsID()
	{
	    $this->selectAdd();
	    $this->selectAdd("news_id");
	    $this->orderBy("news_id DESC");
	    $this->find();
	    if ($this->fetch())
	    {
	        $result = $this->news_id;
	    }
	    else 
	    {
	        $result = 0;
	    }
	    $result++;
	    return $result;
	}
}
?>